FROM python:3.9-slim

# In China, use this
#FROM dockerpull.cn/library/python:3.9-slim

# In China, use apt source - fix for newer Debian systems
#RUN find /etc/apt/ -name "*.sources" -exec sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' {} \;
#RUN find /etc/apt/ -name "*.sources" -exec sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' {} \;

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    ffmpeg \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy requirements first for better caching
COPY requirements.txt .

# In China, use mirror
#RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Create non-root user
RUN useradd --create-home --shell /bin/bash app \
    && chown -R app:app /app
USER app

# Expose port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8000/health || exit 1

# Start the application with gunicorn
# Start API or worker based on the ROLE environment variable
# If ROLE=worker, start worker; otherwise, start API server
CMD ["bash", "-c", "if [ \"$ROLE\" = \"worker\" ]; then bash start.sh worker; else bash start.sh; fi"]
